package cn.xiaomagege.platform.module.sys.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.xiaomagege.platform.module.sys.entity.SysModule;
import cn.xiaomagege.platform.module.sys.service.SysModuleService;
import cn.xiaomagege.platform.util.BreadcrumbUtil;
import cn.xiaomagege.platform.util.PageUtil;
import cn.xiaomagege.platform.vo.Breadcrumb;
import cn.xiaomagege.platform.vo.TablePage;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;

import java.util.List;

@RestController
@RequestMapping("/sys/module")
public class ModuleRestController {
    
    @Autowired
    private SysModuleService sysModuleService;
    
    @Autowired
    private BreadcrumbUtil breadcrumbUtil;
    
    @Autowired
    private PageUtil pageUtil;

    @RequestMapping("/getListPage")
    @RequiresPermissions(value={"sys:module:query"})
    public String list() {
        List<Breadcrumb> breadcrumbs = breadcrumbUtil.getBreadcrumbPath("sys/module/getListPage");
        return JSON.toJSONString(breadcrumbs);
    }
    
    @RequestMapping("/addPage")
    @RequiresPermissions(value={"sys:module:add"})
    public String add() {
        List<Breadcrumb> breadcrumbs = breadcrumbUtil.getBreadcrumbPath("sys/module/addPage");
        return JSON.toJSONString(breadcrumbs);
    }
    
    @RequestMapping("/editPage")
    @RequiresPermissions(value={"sys:module:edit"})
    public String edit() {
        List<Breadcrumb> breadcrumbs = breadcrumbUtil.getBreadcrumbPath("sys/module/editPage");
        return JSON.toJSONString(breadcrumbs);
    }
    
    @RequestMapping("/getModuleForPaging")
    @RequiresPermissions(value={"sys:module:query"})
    public String getModuleForPaging() {
        TablePage tp = pageUtil.getDataForPaging(sysModuleService.getModuleForPaging());
        return JSON.toJSONString(tp);
    }
    
    @RequestMapping("/save")
    @RequiresRoles("admin_sys")
    @RequiresPermissions(value={"sys:module:add","sys:module:edit"}, logical = Logical.OR)
    public String save(@RequestBody SysModule sysModule) {
        int result = sysModuleService.saveModule(sysModule);
        if(result > 0) {
            return JSON.toJSONString("true");
        } else {
            return JSON.toJSONString("false");
        }
    }
    
    @RequestMapping("/delete")
    @RequiresRoles("admin_sys")
    @RequiresPermissions(value={"sys:module:delete"})
    public String delete(@RequestBody String ids) {
        ids = ids.replaceAll("\"","");
        int result = sysModuleService.deleteModuleByIds(ids);
        if(result > 0) {
            return JSON.toJSONString("true");
        } else {
            return JSON.toJSONString("false");
        }
    }
}